<?php

class RequestvideoplaybackController extends Controller
{
        public $layout='//layouts/column2';

        public function filters()
        {
            return array(
                    'accessControl', // perform access control for CRUD operations
                    'postOnly + delete', // we only allow deletion via POST request
            );
        }

        public function accessRules()
        {
            return array(
                    array('allow',  // allow all users to perform 'index' and 'view' actions
                            'actions'=>array(''),
                            'users'=>array('*'),
                    ),
                    array('allow', // allow authenticated user to perform 'create' and 'update' actions
                            'actions'=>array('index','cctvplayvideo'),
                            'users'=>array('@'),
                    ),
                    array('allow', // allow admin user to perform 'admin' and 'delete' actions
                            'actions'=>array(''),
                            'users'=>array('admin'),
                    ),
                    array('deny',  // deny all users
                            'users'=>array('*'),
                    ),
            );
        }
        
        public function actionIndex()
	{
            $model = new Cctvrequestvideoplayback();
            $credentialname = Yii::app()->user->name;
            
            $model->createdby = $credentialname;
            $model->createdon = Date("Y-m-d H:i:s");


            if(isset($_POST['ajax']) && $_POST['ajax']==='client-account-create-form')
            {
                echo CActiveForm::validate($model);
                Yii::app()->end();
            }

            if(isset($_POST['Cctvrequestvideoplayback']))
            {
                $model->attributes=$_POST['Cctvrequestvideoplayback'];
                if($model->validate())
                {
                    $cctvreq = Cctvrequestvideoplayback::model()->find("credentialname = '$credentialname' and computername = '$model->computername' and cameraname = '$model->cameraname' and isactive = 'true'");
                    
                    if(count($cctvreq)<=0){
                        $model->isNewRecord= TRUE;
                    
                        $criteria=new CDbCriteria;
                        $criteria->condition = "credentialname = '{$credentialname}' and computername = '{$model->computername}' and cameraname = '{$model->cameraname}'";
                        $criteria->order = "requestid DESC";
                        $rvid = Cctvrequestvideoplayback::model()->find($criteria);
                        $maxvalue = isset($rvid)?$rvid->requestid:0;
                        $model->foldername = md5($credentialname.$model->computername.$model->cameraname.Date("Y-m-d H:i:s"));
                        $model->requestid = $maxvalue+1;
                        $model->credentialname = $credentialname;
                        $model->isactive = 1;
                    }else{
                        $cctvreq->startfrom = $model->startfrom;
                                
                        $cctvreq->foldername = md5($credentialname.$model->computername.$model->cameraname.Date("Y-m-d H:i:s"));
                        $cctvreq->filestransfered = 0;
                        $cctvreq->isfilestransferedcomplete = 0;

                        $model->isNewRecord= FALSE;
                        $model = $cctvreq;
                    }
                    
                    if($this->saveModel($model)){
                        $this->redirect(array("cctvplayvideo", 'ComputerName'=>$model->computername, 'CameraName'=>$model->cameraname));
                    }
                }
            }
                
            $this->render('index', array('model'=>$model));
	}

        public function actionCctvplayvideo($ComputerName, $CameraName){   
            $cctvreqplayback = new Cctvrequestvideoplayback();
            
            $this->render('cctvplayvideo', array('co'=>$ComputerName, 'ca'=>$CameraName));
        }
        
        public function loadModel($RequestID, $CredentialName, $ComputerName, $CameraName)
	{
                $model=Cctvrequestvideoplayback::model()->findByPk(array('RequestID'=>$RequestID, 'CredentialName'=>$CredentialName, 'ComputerName'=>$ComputerName, 'CameraName'=>$CameraName));
		if($model==null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param Cctvrequestvideo $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='cctvrequestvideoplayback-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
        
        public function saveModel($model)
	{
            try
            {
                    return $model->save();
            }
            catch(Exception $e)
            {
                    $this->showError($e);
            }		
	}
        
        function showError(Exception $e)
	{
		if($e->getCode()==23000)
			$message = "This operation is not permitted due to an existing foreign key reference.";
		else
			$message = "Invalid operation.";
		throw new CHttpException($e->getCode(), $message);
	}
        
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}